import { NextRequest, NextResponse } from 'next/server'

export async function POST(request: NextRequest) {
  const { username, password } = await request.json()

  const res = await fetch('https://imartuat.kinlong.ph:9091/api/v1/user/login?nation_code=PH', {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json'
    },
    body: JSON.stringify({ username, password })
  })
  
  const data = await res.json()
  
  return new Response(JSON.stringify({
    code: data.code,
    msg: data.msg,
  }), {
    status: 200,
    headers: {
      'Set-Cookie': `token=${data.data.token}; Path=/; HttpOnly; Secure; SameSite=Strict`,
    }
  })
}
